﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LogLib;

namespace EECMISWS
{
    public class AppointmentInfo
    {
        /// <summary>
        /// 预约信息
        /// </summary>
        private List<AppointmentApplyInfo> listApplyInfos = new List<AppointmentApplyInfo>();

        public List<AppointmentApplyInfo> ApplyInfos
        {
            get { return listApplyInfos; }
        }

        /// <summary>
        /// 港澳亲属信息
        /// </summary>
        private List<RelationPeopleInfo> gxrInfoList = new List<RelationPeopleInfo>();

        public RelationPeopleInfo GXRInfo
        {
            get
            {
                if (gxrInfoList.Count > 0)
                {
                    return gxrInfoList[0];
                }
                else
                {
                    return null;
                }
            }
        }

        public bool IsEmpty
        {
            get
            {
                return listApplyInfos.Count == 0;
            }
        }

        private AppointmentApplyInfo GetApplyInfo(string sqlb)
        {
            int index = listApplyInfos.FindIndex(r => sqlb.Equals(r.sqlb));
            if (index >= 0)
            {
                return listApplyInfos[index];
            }
            else
            {
                return null;
            }
        }

        public bool ContainsSQ_HZ
        {
            get { return listApplyInfos.FindIndex(r => "101".Equals(r.sqlb)) >= 0; }
        }

        public bool ContainsSQ_GAZ
        {
            get { return listApplyInfos.FindIndex(r => "102".Equals(r.sqlb)) >= 0; }
        }

        public bool ContainsSQ_TWZ
        {
            get { return listApplyInfos.FindIndex(r => "104".Equals(r.sqlb)) >= 0; }
        }

        public AppointmentApplyInfo HZ
        {
            get
            {
                return GetApplyInfo("101");
            }
        }

        public AppointmentApplyInfo GAZ
        {
            get
            {
                return GetApplyInfo("102");
            }
        }

        public AppointmentApplyInfo TWZ
        {
            get
            {
                return GetApplyInfo("104");
            }
        }

        private List<AppointmentQZInfo> listQZInfos = new List<AppointmentQZInfo>();
        public List<AppointmentQZInfo> QZInfos
        {
            get { return listQZInfos; }
        }

        public List<AppointmentQZInfo> SCZQZ
        {
            get
            {
                return listQZInfos.Where(r => r.qwd == "HKG" || r.qwd == "MAC").ToList();
            }
        }

        public bool ContainsQZ_HKG
        {
            get { return listQZInfos.FindIndex(r => "HKG".Equals(r.qwd)) >= 0; }
        }

        public bool ContainsQZ_MAC
        {
            get { return listQZInfos.FindIndex(r => "MAC".Equals(r.qwd)) >= 0; }
        }

        public bool ContainsQZ_TWN
        {
            get { return listQZInfos.FindIndex(r => "TWN".Equals(r.qwd)) >= 0; }
        }

        private AppointmentQZInfo GetQZInfo(string qwd)
        {
            int index = listQZInfos.FindIndex(r => qwd.Equals(r.qwd));
            if (index >= 0)
            {
                return listQZInfos[index];
            }
            else
            {
                return null;
            }
        }

        public AppointmentQZInfo HKG_QZ
        {
            get
            {
                return GetQZInfo("HKG");
            }
        }

        public AppointmentQZInfo MAC_QZ
        {
            get
            {
                return GetQZInfo("MAC");
            }
        }

        public AppointmentQZInfo TWN_QZ
        {
            get
            {
                return GetQZInfo("TWN");
            }
        }

        public void AddApplyInfo(AppointmentApplyInfo info)
        {
            if (info == null || !info.IsValid)
            {
                Log.WriteAsync("无效的申请信息:");
                return;
            }
            if (listApplyInfos.Count == 0)
            {
                listApplyInfos.Add(info);
                string strlLog = string.Format("新预约申请信息添加成功，申请号:{0}, 类别{1}, 业务编号{2}", info.sqh, info.sqlb, info.ywbh);
                Log.WriteAsync(strlLog);
                return;
            }
            string sqh = listApplyInfos[0].sqh;
            if (!sqh.Equals(info.sqh))
            {
                Log.WriteAsync("申请信息不属于当次, 无效");
                return;
            }
            if (listApplyInfos.FindIndex(r => info.sqlb.Equals(r.sqlb)) >= 0)
            {
                Log.WriteAsync("申请类别已存在, 无效");
                return;
            }
            listApplyInfos.Add(info);
            string loginfo = string.Format("新预约申请信息添加成功，类别{0}, 业务编号{1}", info.sqlb, info.ywbh);
            Log.WriteAsync(loginfo);
        }

        public void AddQZInfo(AppointmentQZInfo info)
        {
            if (info == null || !info.IsValid)
            {
                Log.WriteAsync("无效的签注申请信息:");
                return;
            }
            if (listApplyInfos.FindIndex(r => info.ywbh.Equals(r.ywbh)) < 0)
            {
                Log.WriteAsync("对应的申请信息不存在, 无效");
                return;
            }
            if (listQZInfos.Count == 0)
            {
                listQZInfos.Add(info);
                string strlLog = string.Format("新预约签注申请信息添加成功，签注类别{0}, 业务编号{1}", info.qzzl, info.ywbh);
                Log.WriteAsync(strlLog);
                return;
            }
            string sqh = listApplyInfos[0].sqh;        
            if (info.qwd == "TWN" && !ContainsSQ_TWZ)
            {
                Log.WriteAsync("对应的申请信息不存在, 无效");
                return;
            }
            if (info.qwd == "HKG" && !ContainsSQ_GAZ)
            {
                Log.WriteAsync("对应的申请信息不存在, 无效");
                return;
            }
            if (info.qwd == "MAC" && !ContainsSQ_GAZ)
            {
                Log.WriteAsync("对应的申请信息不存在, 无效");
                return;
            }
            listQZInfos.Add(info);
            string loginfo = string.Format("新预约签注信息添加成功，签注种类{0}, 业务编号{1}", info.qzzl, info.ywbh);
            Log.WriteAsync(loginfo);
        }

        public void AddRelationPeopleInfo(RelationPeopleInfo info)
        {
            if (info == null || !info.IsValid)
            {
                Log.WriteAsync("无效的关系人信息:");
                return;
            }
            if (listApplyInfos.FindIndex(r => info.ywbh.Equals(r.ywbh)) < 0)
            {
                Log.WriteAsync("对应的申请信息不存在, 无效");
                return;
            }
            if (gxrInfoList.Count == 0)
            {
                gxrInfoList.Add(info);
                string strlLog = string.Format("新关系人信息添加成功，业务编号{0}", info.ywbh);
                Log.WriteAsync(strlLog);
                return;
            }
            gxrInfoList.Add(info);
            string loginfo = string.Format("新关系人信息添加成功，业务编号{0}", info.ywbh);
            Log.WriteAsync(loginfo);
        }
    }
}
